import com.wcarmon.life.event.*;
import com.wcarmon.life.model.LifeModelLocator;
import flash.utils.Timer;
import mx.binding.utils.ChangeWatcher;
import mx.collections.*;
import mx.events.*;
import mx.utils.*;

include "../../../includes/loggerSnippet.as";



[Bindable]
public var blocksToProcess : Array;

[Bindable]
private var model : LifeModelLocator = LifeModelLocator.getInstance();

private var lastClickTime : int = -1;
private var lastClickedId : int = -1;

public var MAX_ROW_COUNT_FOR_GRID : uint = 20;




///////////////////////////////////////////////////////////////
private function doInit() : void{
	ChangeWatcher.watch( model, "blocksToProcessAreLoaded", selectSpecificBlockIdToProcess );
}


private function selectSpecificBlockIdToProcess( event: Event ) : void{	
	if( model.blocksToProcessAreLoaded ){
		if( -1 == model.specificBlockIdToProcess ){
			log.debug('processing first item in list');
			
			collectBoxGrid.selectedIndex = 0;
			var b_id : uint = collectBoxGrid.selectedItem.id;
			log.debug('first id in list is block id #' + b_id);
			( new GetBlockForProcessingEvent(b_id) ).dispatch();
		}else{
			log.debug('processing block #' + model.specificBlockIdToProcess );
			( new GetBlockForProcessingEvent(model.specificBlockIdToProcess) ).dispatch();
		}
		
	}else{
		log.debug('still waiting for process items list to get data');
	}
}	



///////////////////////////////////////////////////////////////
private function itemClickEvent(event:ListEvent):void {
	log.debug( "item clicked on process blocks grid:  col=" + event.columnIndex + ", row=" + event.rowIndex + ", event type=" + event.type + ", selectedItem=" + event.target.selectedItem);
            
	var hasItem : Boolean = ( event.target.selectedItem != null );
	var isDoubleClick : Boolean = (getTimer() - lastClickTime) < LifeModelLocator.DOUBLE_CLICK_THRESHOLD_IN_MS;
	var isSameItem : Boolean = false; 
	var block_id : uint;
	
	if( 'itemClick' == event.type && hasItem ){
       	block_id = event.target.selectedItem.id;
		
		isSameItem = (lastClickedId == block_id);
		lastClickTime = getTimer();
		lastClickedId = block_id; 
	}
           
	if( isDoubleClick && isSameItem ){
		log.debug("Sending the request over the wall...[id# "+ block_id +"]");
		( new GetBlockForProcessingEvent(block_id) ).dispatch();
	} 
}			




///////////////////////////////////////////////////////////////
private function getCollectBlocks(a : Array) : ArrayCollection {
	var ans : ArrayCollection = new ArrayCollection( a );				
	
	if( a.length > 0 ){
		var sorter : Sort = new Sort();
		var sf1 : SortField = new SortField("created", false, false);
		var sf2 : SortField = new SortField("id", false, false);
		sorter.fields = [sf1,sf2];
		ans.sort = sorter;
		ans.refresh();
		collectBoxGrid.rowCount = Math.min( MAX_ROW_COUNT_FOR_GRID, a.length+1 );
	}else{
		collectBoxGrid.rowCount = 2;
	}
	return ans;
}



///////////////////////////////////////////////////////////////
private function formatDates(item:Object, column:DataGridColumn) : String{
	return myDF1.format( item[column.dataField] );
}
private function content_dataTipFunc(item:Object):String {
	return item.content.substr(0, 300);
}			
